package com.nuc.bigdata.app.service.impl;

import com.nuc.bigdata.app.service.ShopCarService;
import com.nuc.bigdata.domain.vo.ShopCarGoodsVO;
import com.nuc.bigdata.domain.vo.ShopCarVO;
import com.nuc.bigdata.feign.CommodityServiceFeign;
import com.nuc.bigdata.feign.PictureServiceFeign;
import com.nuc.bigdata.feign.ShopCarServiceFeign;
import com.nuc.bigdata.feign.dto.ShopCarGoodsDto;
import com.nuc.bigdata.feign.entity.CommodityDetailFeign;
import com.nuc.bigdata.feign.entity.CommodityFeign;
import com.nuc.bigdata.feign.entity.ShopCarFeign;
import com.nuc.bigdata.feign.entity.ShopCarGoodsFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedList;

/**
 * @author JackLuo
 * @date 2019/12/5
 */
@Service
public class ShopCarServiceImpl implements ShopCarService {
    private final ShopCarServiceFeign carServiceFeign;
    private final CommodityServiceFeign commodityServiceFeign;
    private final PictureServiceFeign pictureServiceFeign;

    @Autowired
    ShopCarServiceImpl(ShopCarServiceFeign carServiceFeign,
                       CommodityServiceFeign commodityServiceFeign,
                       PictureServiceFeign pictureServiceFeign) {
        this.carServiceFeign = carServiceFeign;
        this.commodityServiceFeign = commodityServiceFeign;
        this.pictureServiceFeign = pictureServiceFeign;
    }
    /**
     * 根据用户唯一标识获取购物车信息
     *
     * @param userId 用户唯一标识
     * @return 购物车feign实体对象
     */
    @Override
    public ShopCarVO queryCarFeignByUserId(String userId) {
        ShopCarFeign shopCarFeign = carServiceFeign.queryByUserId(userId);
        return transformShopCar(shopCarFeign);
    }

    /**
     * 将购物车信息转化为VO
     * @param feign 购物车feign
     * @return 购物车VO
     */
    private ShopCarVO transformShopCar(ShopCarFeign feign) {
        ShopCarVO shopCarVO = new ShopCarVO();
        shopCarVO.setShopCarId(feign.getShopCarId());
        ArrayList<ShopCarGoodsVO> vos = new ArrayList<>();
        double amount =0;
        LinkedList<ShopCarGoodsFeign> goodsLinkedList = feign.getGoodsLinkedList();
        for (ShopCarGoodsFeign goodsFeign: goodsLinkedList) {
            ShopCarGoodsVO shopCarGoodsVO = transformShopCarGoods(goodsFeign);
            BigDecimal voAmount = shopCarGoodsVO.getAmount();
            vos.add(shopCarGoodsVO);
            double goodsAmount = voAmount.doubleValue();
            amount = goodsAmount +amount;
        }
        shopCarVO.setCarGoods(vos);
        shopCarVO.setAmount(amount);
        return shopCarVO;
    }

    /**
     * 将购物车中商品转化为VO
     * @param feign 商品
     * @return 商品VO
     */
    private ShopCarGoodsVO transformShopCarGoods(ShopCarGoodsFeign feign) {
        ShopCarGoodsVO vo = new ShopCarGoodsVO();
        // 转化
        vo.setCommodityId(feign.getCommodityId());
        Long commodityQuantity = feign.getCommodityQuantity();
        vo.setCommodityQuantity(commodityQuantity);
        vo.setId(feign.getId());
        vo.setSelected(feign.getSelected());
        String commodityId = feign.getCommodityId();
        CommodityDetailFeign commodityDetailFeign = commodityServiceFeign.queryDetailByCommodityId(commodityId);
        CommodityFeign commodityFeign = commodityServiceFeign.queryCommodityFeignById(commodityId);
        BigDecimal sellPrice = commodityDetailFeign.getSellPrice();
        BigDecimal quantity = new BigDecimal(commodityQuantity);
        vo.setAmount(quantity.multiply(sellPrice));
        vo.setSellPrice(sellPrice);
        vo.setTitle(commodityFeign.getTitle());
        String homePic = commodityFeign.getHomePic();
        vo.setPictureUrl(pictureServiceFeign.getThumbnailUrl(homePic));
        return vo;
    }

    /**
     * 添加单种商品到购物车
     *
     * @param goodsDto 商品dto对象的
     * @param userId   用户唯一标志
     */
    @Override
    public void addGoodsIntoCar(ShopCarGoodsDto goodsDto, String userId) {
        carServiceFeign.addGoodsIntoCar(goodsDto, userId);
    }

    /**
     * 移除一种商品
     *
     * @param commodityId 商品唯一标识
     * @param userId      用户唯一标识
     */
    @Override
    public void removeGoodsFromCar(String commodityId, String userId) {
        carServiceFeign.removeGoodsFromCar(commodityId, userId);
    }

    /**
     * 更新购物车中商品数量
     *
     * @param commodityId 商品唯一标识
     * @param userId      用户唯一标识
     * @param quantity    数量
     */
    @Override
    public void updateGoodsInCar(String commodityId, String userId, Long quantity) {
        carServiceFeign.updateGoodsInCar(commodityId, userId, quantity);
    }

    /**
     * 更新购物车中商品是否被选中
     *
     * @param commodityId 商品唯一标识
     * @param userId      用户唯一标识
     * @param selected    是否被选中
     */
    @Override
    public void updateGoodsSelectedCar(String commodityId, String userId, Boolean selected) {
        carServiceFeign.updateGoodsSelectedInCar(commodityId, userId, selected);
    }
}
